﻿Public Class Settings
    Private isactivated As Boolean = False
    Private Sub btncancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncancel.Click
        My.Settings.Reload()
        Me.Close()
    End Sub

    
    Private Sub btnsave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsave.Click
        For Each tp As TabPage In tabctrl.TabPages
            Dim tg As String = tp.Tag
            If (Not applyByTag(tg)) Then Return
        Next
        My.Settings.Save()
        asService()
        Me.Close()
    End Sub
    Private Sub asService()
        Dim si As New ProjectInstaller
        If (My.Settings.op_autorun) Then
            Try
                ' si.Install(New Hashtable)
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try

        Else
            Try
                'si.Uninstall(New Hashtable)
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try


        End If

    End Sub
    Public Sub loadConfiguration()
        uclogsettin.loadConfig()
        ucmysqlsetting.loadConfig()
        ucwinccsetting.loadConfig()
        ucopsetting.loadConfig()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim tg As String = tabctrl.SelectedTab.Tag
        applyByTag(tg)
    End Sub
    Private Function applyByTag(ByVal tg As String) As Boolean
        If (String.IsNullOrEmpty(tg)) Then Return True
        Dim uch As IConfHolder = Nothing
        If (tg = commons.MOD_LOGGER_SETTING) Then
            Try
                uch = CType(Me.uclogsettin, IConfHolder)
            Catch ex As Exception
                uch = Nothing
            End Try
        End If
        If (tg = commons.MOD_MYSQL_SETTING) Then
            Try
                uch = CType(Me.ucmysqlsetting, IConfHolder)
            Catch ex As Exception
                uch = Nothing
            End Try

        End If
        If (tg = commons.MOD_WINCC_SETTING) Then
            Try
                uch = CType(Me.ucwinccsetting, IConfHolder)
            Catch ex As Exception
                uch = Nothing
            End Try

        End If
        If (tg = commons.MOD_OPERATION_SETTING) Then
            Try
                uch = CType(Me.ucopsetting, IConfHolder)
            Catch ex As Exception
                uch = Nothing
            End Try

        End If
        If (Not IsNothing(uch)) Then
            Dim ret As Boolean = uch.applyConfig
            If (Not ret) Then
                Dim mret = commons.yesNoCancelMsg(tg, uch.validationerrors)
                Select Case mret
                    Case vbCancel
                        uch.revertconfig()
                        Return False
                    Case vbYes
                        uch.forcewrite()
                        Return True
                    Case Else
                        Return False
                End Select
            End If
        End If
        Return True
    End Function

    Private Sub Settings_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
        If (Not isactivated) Then
            isactivated = True
            Timer1.Enabled = True
            Timer1.Start()

        End If
    End Sub

    Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Timer1.Stop()
        Timer1.Enabled = False
        Me.Cursor = Cursors.AppStarting
        ' Me.UseWaitCursor = True
        'My.Application.DoEvents()
        loadConfiguration()
        Me.Cursor = Cursors.Default
        'My.Application.DoEvents()
        'Me.UseWaitCursor = False
    End Sub
End Class